<template>
  <div class="box">
    <div class="top">
      <span @click="router.push('/home')">&lt;</span>
      <form action="/">
        <van-search
          v-model="name"
          show-action
          placeholder="请输入商品关键词"
          @search="onSearch"
          @cancel="onCancel"
        />
      </form>
    </div>
    <div class="center">
      <div class="left">
        <ul v-for="(item, index) in list.listData" :key="index">
          <li
            :class="active == index + 1 ? 'active' : ''"
            @click="qiehuan(item.id)"
          >
            {{ item.title }}
          </li>
        </ul>
      </div>
      <div class="right">
        <div v-for="(item, index) in list.rightData" :key="index">
          <div @click="toitem(item.id)">
            <van-card
              :num="item.num"
              :price="item.price"
              :desc="item.desc"
              :title="item.name"
              :thumb="item.img"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang='ts' setup>
import { getleftlist, getrightlist, search } from "../../../api/api";
import { ref, reactive, onMounted } from "vue";
import { useRouter } from "vue-router";

const router = useRouter();

onMounted(() => {
  render();
});

// 数据
const list: any = reactive({
  listData: [],
  rightData: [],
});

// 搜索框的值
const name = ref("");

// 右侧id
const ids = ref(1);

// 渲染
const render = () => {
  getleftlist().then((res) => {
    list.listData = res.data.data;
  });
  getrightlist(ids.value).then((res) => {
    list.rightData = res.data.data;
  });
};

// 高亮
const active = ref(1);

// 点击左切换右
const qiehuan = (id: number) => {
  active.value = id;
  ids.value = id;
  render();
};

// 跳转到详情
const toitem = (id: number) => {
  router.push(`/item/${id}`);
};

// 搜索
const onSearch = () => {
  search(name.value).then((res) => {
    list.rightData = res.data.data;
  });
};

const onCancel = () => {
  render();
};
</script>

<style scoped>
::-webkit-scrollbar {
  display: none;
}
h2 {
  margin-bottom: 0px;
  text-align: center;
}
.childrentitle {
  font-size: 16px;
  display: inline-block;
  margin-left: 15px;
  text-align: center;
  margin-top: 5px;
  /* justify-content: space-around; */
}
.left {
  float: left;
  width: 28%;
  /* border-right: 1px solid #ccc; */
  overflow: auto;
  height: calc(100vh - 100px);
  background-color: #f9f9f9;
}
li {
  height: 50px;
  font-size: 13px;
  padding: 5px;
  line-height: 50px;
  border-bottom: 1px solid #e6e6e6;
}
.right {
  overflow: auto;
  float: right;
  width: 71%;
  height: calc(100vh - 100px);
  /* border: 1px solid #ccc; */
}
.title {
  font-size: 13px;
  color: #9f9f9f;
  text-align: center;
  line-height: 30px;
}
.active {
  color: red;
}
.top {
  height: 50px;
  line-height: 50px;
}
.top span {
  font-size: 20px;
  margin-left: 10px;
  color: #848484;
  float: left;
}
.box {
  overflow: hidden;
}
.center {
  height: calc(100vh - 100px);
  overflow: auto;
}
.van-action-bar {
  position: fixed;
  bottom: 0px;
}
</style>
